package com.example.bootDemo.config;

import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.scheduling.quartz.AdaptableJobFactory;
import org.springframework.stereotype.Component;

/**
 * @description: 创建job 实例工厂 解决spring注入问题，如果使用默认会导致spring的@Autowired 无法注入问题
 * @author: zys
 * @create: 2021-04-09 15:16
 **/
@Component
public class TaskJobFactory extends AdaptableJobFactory {

    @Autowired
    private AutowireCapableBeanFactory autowireCapableBeanFactory;

    @Override
    protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
        //调用父类方法
        Object object = super.createJobInstance(bundle);
        //进行注入
        autowireCapableBeanFactory.autowireBean(object);
        return object;
    }

}
